//模块定义加载区域 ===================================================

const gulp = require('gulp');//本地gulp的模块定义
const connect = require('gulp-connect');//服务器启动
const imagemin = require('gulp-imagemin');//压缩图片

const {     
      scripts,
      scss : scssJSON,
      proxyList,
} = require('./conf/index')

const {     
      mergeMyScripts,
      watchScripts,
      mergeMyScss,
      watchScss,
      gpProxy,
} = require("./utils/index")

let   [scriptsTaskArray,scssTaskArray] = [];

// 功能区 ===================================================

function init(){
      scriptsTaskArray = mergeMyScripts(scripts, "scripts-", 'dev');
      scssTaskArray = mergeMyScss(scssJSON, 'scss-', 'dev');
}

//指令定义区

/**
 * @task html :  转存html
 * @task connect : 服务器配置
 * @task watch : 监听
 * @task gulp-dev : 自动开启环境
 */

//获取src中所有.html文件------------start---------------
gulp.task('html',()=>{
      return gulp.src(['./src/*.html']).pipe(gulp.dest('./dev')).pipe(connect.reload());
})                                                              //配合connect自动刷新
//获取src中所有.html文件------------end---------------

//获取src中所有.json文件------------start---------------
gulp.task('json',()=>{
      return gulp.src(['./src/json/*.json']).pipe(gulp.dest('./dev/json')).pipe(connect.reload());
})                                                              //配合connect自动刷新
//获取src中所有.json文件------------end---------------

// images获取
gulp.task('img',()=>{
      return gulp.src(['./src/images/**/*.+(jpg|png)'])
      .pipe(imagemin())
      .pipe(gulp.dest('./dev/images'));
})


// 服务器配置
gulp.task('connect',()=>{
      connect.server({
            port: 8888,//端口号
            root: 'dev',
            livereload: true,//自动刷新
            middleware:function(connect, opt){
                  return gpProxy(proxyList);
            }
      })
})

// 监听src中文件的更改并转存到dev中
gulp.task('watch',()=>{
      gulp.watch('./src/*.html',['html']);
      gulp.watch('./src/json/*.json',['json']);
      gulp.watch('./src/images/**/*.+(jpg|png)',['img']);
      watchScripts(scripts , 'scripts-');
      watchScss(scssJSON, 'scss-')
      
})

init();

gulp.task("scripts",scriptsTaskArray);
gulp.task("scss",scssTaskArray);

gulp.task("gulp-dev",["html","json","scripts","scss","img","connect","watch"]);

require("./build");
